// devops/pages/power/device.js
import service from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeList: [
     {
      id: 0, name: '变压器'
    },{
      id: 1, name: '主配电箱'
    },
    {
      id: 2, name: '二级配电箱'
    }],
    deviceList: [{id: 1, name: '设备1'}],
    type: '',
    name: '',
    mark: '',
    address: '',
    site: '',
    pid: 0,
    id: '',
    pidArray: [-99, -99],
    type: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('op', options)
    if(options.id){
      var newArr = []
      if(options.path){
         newArr = options.path.split(",")
        // newArr = arr.slice(1);
        //  console.log(arr)
      }
      this.setData({
        id: options.id,
        pidArray: newArr
      })
      this.getDetail()
    }
  },
  getDetail(){
    let that = this
    service("GET",`/devops/power/box/device/info/${this.data.id}`).then(res => {
      console.log('info', res)
      if(res.data.code == 0){
        // that.getDevice(res.data.pid)
       that.setData({
         type: res.data.info.type,
         name: res.data.info.name,
         mark: res.data.info.name,
         address: res.data.info.address,
         site: res.data.info.site,
         pid: res.data.info.pid,
         type: res.data.info.type,

       })
      }
    })
  },
  getDevice(id){
    let that = this
    service("POST",`/devops/power/box/device/parent/${id}`).then(res => {
      console.log('info', res)
      if(res.data.code == 0){
        let data = res.data.info
        let arr = [id, -99, -99]
        if(data.parent){
          arr = [data.parent.id, id, -99]
          if(data.parent.parent){
            arr = [data.parent.parent.id, data.parent.id, id]
          }
        }
        that.setData({
          pidArray: arr
        })
      }
    })
  },
  confirmType(option){
    this.setData({
      type: this.data.typeList[option.detail].id,
    })
  },
  confirmSite(option){
    this.setData({
      site: option.detail,
    })

  },
  confirmAdd(){
    let name = this.data.name
    let mark = this.data.mark
    let type = this.data.type
    let address = this.data.address
    let siteId = this.data.site 
    let pid = this.data.pid
    if(pid ==0 && !siteId){
      wx.showToast({
        title: '请选择设备或站点',
        icon: 'none'
      })
      return
    }
    console.log('cc', { name, mark, type, address, siteId ,pid })
    // return
    var id= this.data.id
    let url = this.data.id ? '/devops/power/box/device/update' : '/devops/power/box/device/add'
    let postData = this.data.id ? { name, mark, type ,address,siteId, pid, id } : { name, mark, type ,address,siteId, pid }
    service("POST",url,postData).then(res => {
      console.log('info', res)
      if(res.data.code == 0){
        wx.showToast({
          title: '操作成功',
          icon: 'none'
        })
       
        setTimeout(() => {
          wx.navigateBack()
        }, 1000);
      }else{
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  confirmDevice(option){
   console.log(option)
    let arr = option.detail
    let pid = 0
    let type = 0
    if(arr.one !=-99){
      pid = arr.one
      type = 1
    }
    if(arr.two !=-99){
      pid = arr.two
      type = 2
    }
    if(arr.three !=-99){
      pid = arr.three
    }
    console.log('confirmDevice', pid)
    this.setData({
      pid: pid,
      type: type
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})